# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# fishnet_intersection.py
# Created on: 2020-07-24
# Last edited: 2020-08-02
# Description: fishnet intersection 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
import os

# Initialize the ArcGIS Pro license
arcpy.env.overwriteOutput = True

# Check product license (for ArcGIS Pro)
product_info = arcpy.CheckProduct("arcinfo")
if product_info == "Available":
    print("ArcGIS license is available.")
else:
    print(f"ArcGIS license is not available: {product_info}")

arcpy.CheckOutExtension("Spatial")  # For Spatial Analysis extension, if needed

def get_script_path():
    # Get the absolute path of the currently running script
    return os.path.abspath(__file__)

wkdir = os.path.dirname(get_script_path())

# Local variables:
fishnet_lake_chad_ntl_x_d01_shp = "%s\\local\\gis_data\\003_boundaries\\fishnet\\fishnet_lake_chad_ntl_x_d01.shp" % wkdir
lake_chad_adm0_shp = "%s\\local\\gis_data\\003_boundaries\\lake_chad\\lake_chad_adm0.shp" % wkdir
fishnet_lake_chad_ntl_adm0_x_d01_shp = "%s\\local\\gis_data\\003_boundaries\\fishnet\\fishnet_lake_chad_ntl_adm0_x_d01.shp" % wkdir

if False:
    print('recalc intersection')
    if arcpy.Exists(fishnet_lake_chad_ntl_adm0_x_d01_shp):
        arcpy.Delete_management(fishnet_lake_chad_ntl_adm0_x_d01_shp)
    # Process: Intersect
    arcpy.Intersect_analysis("'%s\\local\\gis_data\\003_boundaries\\fishnet\\fishnet_lake_chad_ntl_x_d01.shp' #;'%s\\local\\gis_data\\003_boundaries\\lake_chad\\lake_chad_adm0.shp' #" % (wkdir,wkdir), fishnet_lake_chad_ntl_adm0_x_d01_shp, "NO_FID", "", "INPUT")
  
flyr = "fishnet_lyr"
arcpy.MakeFeatureLayer_management(fishnet_lake_chad_ntl_adm0_x_d01_shp,flyr)

##
## OBJECTID
##
if not arcpy.ListFields(flyr, "OBJECTID"):
    arcpy.AddField_management(flyr, "OBJECTID", "LONG", 12,"","","","NON_NULLABLE")
arcpy.CalculateField_management(flyr,"OBJECTID","!FID!","PYTHON")

##
## add lat/lon from Centroid "inside"
##
if not arcpy.ListFields(flyr, "INSIDE_X"):
    arcpy.AddGeometryAttributes_management(flyr,"CENTROID_INSIDE")

##
## series of overlays
##

# state level from 4 countries: GADM
adm1_shp = r"%s\local\gis_data\003_boundaries\gadm36_lake_chad_ctr_1.shp" % wkdir
if not arcpy.Exists(adm1_shp):
    cmr1 = r'%s\local\gis_data\003_boundaries\gadm36_CMR_shp\gadm36_CMR_1.shp' % wkdir
    ner1 = r'%s\local\gis_data\003_boundaries\gadm36_NER_shp\gadm36_NER_1.shp' % wkdir
    nga1 = r'%s\local\gis_data\003_boundaries\gadm36_NGA_shp\gadm36_NGA_1.shp' % wkdir
    tcd1 = r'%s\local\gis_data\003_boundaries\gadm36_TCD_shp\gadm36_TCD_1.shp' % wkdir
    arcpy.Merge_management([cmr1,ner1,nga1,tcd1],adm1_shp)
    print(f"Merging complete. The merged feature class is located at: {adm1_shp}")
    
adm1="adm1_lyr"
arcpy.MakeFeatureLayer_management(adm1_shp,adm1)

adm2_shp = r"%s\local\gis_data\003_boundaries\gadm36_lake_chad_ctr_2.shp" % wkdir
if not arcpy.Exists(adm2_shp):
    cmr2 = r'%s\local\gis_data\003_boundaries\gadm36_CMR_shp\gadm36_CMR_2.shp' % wkdir
    ner2 = r'%s\local\gis_data\003_boundaries\gadm36_NER_shp\gadm36_NER_2.shp' % wkdir
    nga2 = r'%s\local\gis_data\003_boundaries\gadm36_NGA_shp\gadm36_NGA_2.shp' % wkdir
    tcd2 = r'%s\local\gis_data\003_boundaries\gadm36_TCD_shp\gadm36_TCD_2.shp' % wkdir
    arcpy.Merge_management([cmr2,ner2,nga2,tcd2],adm2_shp)
    print(f"Merging complete. The merged feature class is located at: {adm2_shp}")
    
adm2="adm2_lyr"
arcpy.MakeFeatureLayer_management(adm2_shp,adm2)

adm3_shp = r"%s\local\gis_data\003_boundaries\gadm36_lake_chad_ctr_3.shp" % wkdir
if not arcpy.Exists(adm3_shp):
    #cmr3 = r'%s\local\gis_data\003_boundaries\gadm36_CMR_shp\gadm36_CMR_3.shp' % wkdir
    #ner3 = r'%s\local\gis_data\003_boundaries\gadm36_NER_shp\gadm36_NER_3.shp' % wkdir
    #nga3 = r'%s\local\gis_data\003_boundaries\gadm36_NGA_shp\gadm36_NGA_3.shp' % wkdir
    tcd3 = r'%s\local\gis_data\003_boundaries\gadm36_TCD_shp\gadm36_TCD_3.shp' % wkdir
    arcpy.Merge_management([tcd3],adm3_shp)
    print(f"Merging complete. The merged feature class is located at: {adm3_shp}")
    
adm3="adm3_lyr"
arcpy.MakeFeatureLayer_management(adm3_shp,adm3)

# clean adm3 
keep_fields = ["FID","GID_0","NAME_0","GID_1","NAME_1","NL_NAME_1","GID_2","NAME_2","NL_NAME_2","GID_3","NAME_3"]

fieldNameList = []
fieldObjList = arcpy.ListFields(adm3)

for field in fieldObjList:
    print(field.name)
    if (not field.name in keep_fields) and (not field.required):
        fieldNameList.append(field.name)

if len(fieldNameList)>0:
    arcpy.DeleteField_management(adm3,fieldNameList)

##
## ASSIGN ADMIN 3 identifiers
## sp. join
## https://pro.arcgis.com/en/pro-app/tool-reference/data-management/select-by-location-graphical-examples.htm
##
## method 1. misses 32 fishnet pixels w/ unusual shapes (centroid problem)
## method with polys that use "have_center_in" can be outside of polygon with circutious shapes
## method 2. use inside x,y into points spatial join , calc area, join by max area share
##

flyr_pt_shp = "%s\local\\gis_data\\003_boundaries\\fishnet_pt.shp" % wkdir
#arcpy.Delete_management(flyr_pt_shp)
if not arcpy.Exists(flyr_pt_shp):
    spref = arcpy.Describe(flyr).spatialReference
    arr = arcpy.da.FeatureClassToNumPyArray(flyr, ("OBJECTID","INSIDE_X","INSIDE_Y"))
    # rename OBJECTID to JOINID otherwise reserved var issue #
    arr.dtype.names = ['JOINID', 'INSIDE_X', 'INSIDE_Y']
    outFC = "in_memory//outxy"
    # spref, xy tolerance check 
    arcpy.da.NumPyArrayToFeatureClass(arr, outFC, ['INSIDE_X', 'INSIDE_Y'],spref)
    #flyr_pt = "in_memory//outsjoin"
    # spatial join from points ensures all
    #arcpy.env.XYTolerance = "0.02 Meters"
    arcpy.SpatialJoin_analysis(outFC, adm3, flyr_pt_shp, "#", "KEEP_ALL", "#","INTERSECT","#","CLOSEST")
flyr_pt = "flyr_pt"
arcpy.MakeFeatureLayer_management(flyr_pt_shp,flyr_pt)

if not arcpy.ListFields(flyr_pt, "POINT_X"):
    arcpy.AddXY_management(flyr_pt)

print("added admin vars")


##
## add treatment dummies
## and spatial neighbours neiBH3s1 neiBH3s2... Would do until 5
## NON_NULLABLE forces 0,1
##

sel_method="INTERSECT"
print(sel_method)

##
## BH 3 states ##
bh3s_shp = "%s\\local\\gis_data\\003_boundaries\\bh3s.shp" % wkdir

bh3s = "bh3s_lyr"
arcpy.MakeFeatureLayer_management(bh3s_shp,bh3s)

##
## BH borno ##
bhb_shp = "%s\\local\\gis_data\\003_boundaries\\bhb.shp" % wkdir

bhb = "bhb_lyr"
arcpy.MakeFeatureLayer_management(bhb_shp,bhb)

##
## BH between 2 rivers (benoue and ##
bhbr_shp = "%s\\local\\gis_data\\003_boundaries\\bhbr.shp" % wkdir

bhbr = "bhbr_lyr"
arcpy.MakeFeatureLayer_management(bhbr_shp,bhbr)



##
## ADMIN LEVEL 1
##
admin_level = "s"
bh3s_adm1="bh3s%s" % admin_level
bhb_adm1="bhb%s" % admin_level
bhbr_adm1="bhbr%s" % admin_level
print("s: district level")
print("ADMIN LEVEL 1")

for i in range(1,7):
    print (i)
    
    if (i==1):
        if not arcpy.ListFields(adm1, bh3s_adm1):
            arcpy.AddField_management(adm1, bh3s_adm1, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bh3s_adm1):
            arcpy.AddField_management(flyr_pt,bh3s_adm1, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(flyr_pt,bh3s_adm1,0,"PYTHON")
        arcpy.CalculateField_management(adm1,bh3s_adm1,0,"PYTHON")    
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bh3s)
        arcpy.SelectLayerByLocation_management(adm1, "HAVE_THEIR_CENTER_IN", bh3s)
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bh3s_adm1,(i-1))
        arcpy.SelectLayerByAttribute_management (adm1,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm1))
        arcpy.SelectLayerByLocation_management (adm1, "SHARE_A_LINE_SEGMENT_WITH",adm1,"","NEW_SELECTION")
        #arcpy.SelectLayerByLocation_management (adm1, sel_method, bh3s,"","REMOVE_SELECTION")
        # "bh3s" <= 2 AND "bh3s" >= 1
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bh3s_adm1,(i-1),bh3s_adm1)
        arcpy.SelectLayerByAttribute_management (adm1,"REMOVE_FROM_SELECTION",sqlstmt2)
          # neiBH3d2
        bh3s_i = "nei%s%s" % (bh3s_adm1,(i-1))
        if not arcpy.ListFields(adm1, bh3s_i):
            arcpy.AddField_management(adm1, bh3s_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bh3s_i):
            arcpy.AddField_management(flyr_pt, bh3s_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm1,bh3s_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm1,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bh3s_i,1,"PYTHON")
    arcpy.CalculateField_management(adm1,bh3s_adm1,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bh3s_adm1,i,"PYTHON")

    ## bhb
    print('bhb')
    
    if (i==1):
        if not arcpy.ListFields(adm1, bhb_adm1):
            arcpy.AddField_management(adm1,bhb_adm1, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhb_adm1):
            arcpy.AddField_management(flyr_pt,bhb_adm1, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm1,bhb_adm1,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bhb_adm1,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bhb)
        arcpy.SelectLayerByLocation_management(adm1, "HAVE_THEIR_CENTER_IN", bhb)    
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bhb_adm1,(i-1))
        arcpy.SelectLayerByAttribute_management (adm1,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm1))
        arcpy.SelectLayerByLocation_management (adm1, "SHARE_A_LINE_SEGMENT_WITH",adm1,"","NEW_SELECTION")
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bhb_adm1,(i-1),bhb_adm1)
        arcpy.SelectLayerByAttribute_management (adm1,"REMOVE_FROM_SELECTION",sqlstmt2)
        bhb_i = "nei%s%s" % (bhb_adm1,(i-1))
        if not arcpy.ListFields(adm1, bhb_i):
            arcpy.AddField_management(adm1, bhb_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhb_i):
            arcpy.AddField_management(flyr_pt, bhb_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm1,bhb_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, "INTERSECT", adm1,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bhb_i,1,"PYTHON")
    arcpy.CalculateField_management(adm1,bhb_adm1,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bhb_adm1,i,"PYTHON")

    ## bhbr
    print('bhbr')
    if (i==1):
        if not arcpy.ListFields(adm1, bhbr_adm1):
            arcpy.AddField_management(adm1, bhbr_adm1, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhbr_adm1):
            arcpy.AddField_management(flyr_pt,bhbr_adm1, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm1,bhbr_adm1,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bhbr_adm1,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bhbr)
        arcpy.SelectLayerByLocation_management(adm1, "HAVE_THEIR_CENTER_IN", bhbr)    
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bhbr_adm1,(i-1))
        arcpy.SelectLayerByAttribute_management (adm1,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm1))
        arcpy.SelectLayerByLocation_management (adm1, "SHARE_A_LINE_SEGMENT_WITH",adm1,"","NEW_SELECTION")
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bhbr_adm1,(i-1),bhbr_adm1)
        arcpy.SelectLayerByAttribute_management (adm1,"REMOVE_FROM_SELECTION",sqlstmt2)
        bhbr_i = "nei%s%s" % (bhbr_adm1,(i-1))
        if not arcpy.ListFields(adm1, bhbr_i):
            arcpy.AddField_management(adm1, bhbr_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhbr_i):
            arcpy.AddField_management(flyr_pt, bhbr_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm1,bhbr_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm1,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bhbr_i,1,"PYTHON")
    arcpy.CalculateField_management(adm1,bhbr_adm1,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bhbr_adm1,i,"PYTHON")


##
## assign admin 1 identifiers to fishnet
##    

##
## ADMIN LEVEL 2
##
admin_level = "d"
bh3s_adm2="bh3s%s" % admin_level
bhb_adm2="bhb%s" % admin_level
bhbr_adm2="bhbr%s" % admin_level
print("d: district level")
for i in range(1,7):
    print ("nei:%s" %i)
    print('bh3s')
    if (i==1):
        if not arcpy.ListFields(adm2, bh3s_adm2):
            arcpy.AddField_management(adm2,bh3s_adm2, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bh3s_adm2):
            arcpy.AddField_management(flyr_pt,bh3s_adm2, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm2,bh3s_adm2,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bh3s_adm2,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bh3s)
        arcpy.SelectLayerByLocation_management(adm2, "HAVE_THEIR_CENTER_IN", bh3s)
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bh3s_adm2,(i-1))
        arcpy.SelectLayerByAttribute_management (adm2,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm2))
        arcpy.SelectLayerByLocation_management (adm2, "SHARE_A_LINE_SEGMENT_WITH",adm2,"","NEW_SELECTION")
        # "bh3s" <= 2 AND "bh3s" >= 1
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bh3s_adm2,(i-1),bh3s_adm2)
        arcpy.SelectLayerByAttribute_management (adm2,"REMOVE_FROM_SELECTION",sqlstmt2)
        bh3s_i = "nei%s%s" % (bh3s_adm2,(i-1))
        if not arcpy.ListFields(adm2, bh3s_i):
            arcpy.AddField_management(adm2, bh3s_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bh3s_i):
            arcpy.AddField_management(flyr_pt, bh3s_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm2,bh3s_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm2,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bh3s_i,1,"PYTHON")
    arcpy.CalculateField_management(adm2,bh3s_adm2,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bh3s_adm2,i,"PYTHON")

    ## bhb
    print('bhb')
    
    if (i==1):
        if not arcpy.ListFields(adm2, bhb_adm2):
            arcpy.AddField_management(adm2,bhb_adm2, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhb_adm2):
            arcpy.AddField_management(flyr_pt,bhb_adm2, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm2,bhb_adm2,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bhb_adm2,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bhb)
        arcpy.SelectLayerByLocation_management(adm2, "HAVE_THEIR_CENTER_IN", bhb)
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bhb_adm2,(i-1))
        arcpy.SelectLayerByAttribute_management (adm2,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm2))
        arcpy.SelectLayerByLocation_management (adm2, "SHARE_A_LINE_SEGMENT_WITH",adm2,"","NEW_SELECTION")
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bhb_adm2,(i-1),bhb_adm2)
        arcpy.SelectLayerByAttribute_management (adm2,"REMOVE_FROM_SELECTION",sqlstmt2)
        bhb_i = "nei%s%s" % (bhb_adm2,(i-1))
        if not arcpy.ListFields(adm2, bhb_i):
            arcpy.AddField_management(adm2, bhb_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhb_i):
            arcpy.AddField_management(flyr_pt, bhb_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm2,bhb_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm2,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bhb_i,1,"PYTHON")
    arcpy.CalculateField_management(adm2,bhb_adm2,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bhb_adm2,i,"PYTHON")

    ## bhbr
    print('bhbr')
    if (i==1):
        if not arcpy.ListFields(adm2, bhbr_adm2):
            arcpy.AddField_management(adm2, bhbr_adm2, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhbr_adm2):
            arcpy.AddField_management(flyr_pt,bhbr_adm2, "SHORT", 1,"","","","NON_NULLABLE")
        # reset 0
        arcpy.CalculateField_management(adm2,bhbr_adm2,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bhbr_adm2,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bhbr)
        arcpy.SelectLayerByLocation_management(adm2, "HAVE_THEIR_CENTER_IN", bhbr)
        
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bhbr_adm2,(i-1))
        arcpy.SelectLayerByAttribute_management (adm2,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm2))
        arcpy.SelectLayerByLocation_management (adm2, "SHARE_A_LINE_SEGMENT_WITH",adm2,"","NEW_SELECTION")
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bhbr_adm2,(i-1),bhbr_adm2)
        arcpy.SelectLayerByAttribute_management (adm2,"REMOVE_FROM_SELECTION",sqlstmt2)
        bhbr_i = "nei%s%s" % (bhbr_adm2,(i-1))
        if not arcpy.ListFields(adm2, bhbr_i):
            arcpy.AddField_management(adm2, bhbr_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhbr_i):
            arcpy.AddField_management(flyr_pt, bhbr_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm2,bhbr_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm2,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bhbr_i,1,"PYTHON")
    arcpy.CalculateField_management(adm2,bhbr_adm2,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bhbr_adm2,i,"PYTHON")

##
## ADMIN LEVEL 3
##
print("ADMIN LEVEL 3")
print("c: commune level")
admin_level="c"
bh3s_adm3="bh3s%s" %admin_level
bhb_adm3="bhb%s"%admin_level
bhbr_adm3="bhbr%s"%admin_level

for i in range(1,7):
    print ("nei:%s" %i)
    
    if (i==1):
        if not arcpy.ListFields(adm3, bh3s_adm3):
            arcpy.AddField_management(adm3,bh3s_adm3, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bh3s_adm3):
            arcpy.AddField_management(flyr_pt,bh3s_adm3, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm3,bh3s_adm3,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bh3s_adm3,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bh3s)
        arcpy.SelectLayerByLocation_management(adm3, "HAVE_THEIR_CENTER_IN", bh3s)
        
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bh3s_adm3,(i-1))
        arcpy.SelectLayerByAttribute_management (adm3,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm3))
        arcpy.SelectLayerByLocation_management (adm3, "SHARE_A_LINE_SEGMENT_WITH",adm3,"","NEW_SELECTION")
        #arcpy.SelectLayerByLocation_management (adm1, sel_method, bh3s,"","REMOVE_SELECTION")
        # "bh3s" <= 2 AND "bh3s" >= 1
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bh3s_adm3,(i-1),bh3s_adm3)
        arcpy.SelectLayerByAttribute_management (adm3,"REMOVE_FROM_SELECTION",sqlstmt2)
        bh3s_i = "nei%s%s" % (bh3s_adm3,(i-1))
        if not arcpy.ListFields(adm3, bh3s_i):
            arcpy.AddField_management(adm3, bh3s_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bh3s_i):
            arcpy.AddField_management(flyr_pt, bh3s_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm3,bh3s_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm3,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bh3s_i,1,"PYTHON")
    arcpy.CalculateField_management(adm3,bh3s_adm3,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bh3s_adm3,i,"PYTHON")

    ## bhb
    print('bhb')
    
    if (i==1):
        if not arcpy.ListFields(adm3, bhb_adm3):
            arcpy.AddField_management(adm3,bhb_adm3, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhb_adm3):
            arcpy.AddField_management(flyr_pt,bhb_adm3, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm3,bhb_adm3,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bhb_adm3,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bhb)
        arcpy.SelectLayerByLocation_management(adm3, "HAVE_THEIR_CENTER_IN", bhb)
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bhb_adm3,(i-1))
        arcpy.SelectLayerByAttribute_management (adm3,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm3))
        arcpy.SelectLayerByLocation_management (adm3, "SHARE_A_LINE_SEGMENT_WITH",adm3,"","NEW_SELECTION")
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bhb_adm3,(i-1),bhb_adm3)
        arcpy.SelectLayerByAttribute_management (adm3,"REMOVE_FROM_SELECTION",sqlstmt2)
        bhb_i = "nei%s%s" % (bhb_adm3,(i-1))
        if not arcpy.ListFields(adm3, bhb_i):
            arcpy.AddField_management(adm3, bhb_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhb_i):
            arcpy.AddField_management(flyr_pt, bhb_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm3,bhb_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm3,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bhb_i,1,"PYTHON")
    arcpy.CalculateField_management(adm3,bhb_adm3,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bhb_adm3,i,"PYTHON")

    ## bhbr
    print('bhbr')
    if (i==1):
        if not arcpy.ListFields(adm3, bhbr_adm3):
            arcpy.AddField_management(adm3, bhbr_adm3, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhbr_adm3):
            arcpy.AddField_management(flyr_pt,bhbr_adm3, "SHORT", 1,"","","","NON_NULLABLE")
        arcpy.CalculateField_management(adm3,bhbr_adm3,0,"PYTHON")
        arcpy.CalculateField_management(flyr_pt,bhbr_adm3,0,"PYTHON")
        arcpy.SelectLayerByLocation_management(flyr_pt, sel_method, bhbr)
        arcpy.SelectLayerByLocation_management(adm3, "HAVE_THEIR_CENTER_IN", bhbr)
    else:
        # select previous bh nei #
        sqlstmt = ' "%s" = %s ' % (bhbr_adm3,(i-1))
        arcpy.SelectLayerByAttribute_management (adm3,"NEW_SELECTION",sqlstmt)
        print("count %s" % arcpy.GetCount_management(adm3))
        arcpy.SelectLayerByLocation_management (adm3, "SHARE_A_LINE_SEGMENT_WITH",adm3,"","NEW_SELECTION")
        sqlstmt2 = ' "%s" <= %s AND "%s" >= 1 ' % (bhbr_adm3,(i-1),bhbr_adm3)
        arcpy.SelectLayerByAttribute_management (adm3,"REMOVE_FROM_SELECTION",sqlstmt2)
        bhbr_i = "nei%s%s" % (bhbr_adm3,(i-1))
        print(bhbr_i)
        if not arcpy.ListFields(adm3, bhbr_i):
            arcpy.AddField_management(adm3, bhbr_i, "SHORT", 1,"","","","NON_NULLABLE")
        if not arcpy.ListFields(flyr_pt, bhbr_i):
            arcpy.AddField_management(flyr_pt, bhbr_i, "SHORT", 1,"","","","NON_NULLABLE")
        # dummy #
        arcpy.CalculateField_management(adm3,bhbr_i,1,"PYTHON")
        arcpy.SelectLayerByLocation_management (flyr_pt, sel_method, adm3,"","NEW_SELECTION")
        arcpy.CalculateField_management(flyr_pt,bhbr_i,1,"PYTHON")
    arcpy.CalculateField_management(adm3,bhbr_adm3,i,"PYTHON")
    arcpy.CalculateField_management(flyr_pt,bhbr_adm3,i,"PYTHON")


print('merge with JOINID')
if not arcpy.ListFields(flyr_pt, "INSIDE_X"):
    arcpy.AddGeometryAttributes_management(flyr,"CENTROID_INSIDE")

##
## copy to LATEST adm3 fishnet
##
print("spatial join")
for field in arcpy.ListFields(fishnet_lake_chad_ntl_adm0_x_d01_shp):
    if (not field.name in "OBJECTID") and (not field.required):
        arcpy.DeleteField_management(fishnet_lake_chad_ntl_adm0_x_d01_shp,[str(field.name)])
# merge treatment variables from point based calculations into the fishnet polygons #
newFieldList = []
for field in arcpy.ListFields(flyr_pt):
    if (not field.name in ["TARGET_FID","CLOSEST","Join_Count","NL_NAME_1","NL_NAME_2"]) and (not field.required):
        newFieldList.append(str(field.name))
print("join takes a long time")
arcpy.JoinField_management(fishnet_lake_chad_ntl_adm0_x_d01_shp, "OBJECTID", flyr_pt, "JOINID", newFieldList)

